package com.test.template.publish.config.fac;

import com.elecbook.content.model.po.Contribute;
import com.test.template.publish.config.JsClassConfig;
import com.test.template.publish.config.ServiceMapperConfig;
import com.test.template.publish.config.manager.VelocityConfigManager;
import com.test.template.tool.Tool;

import java.util.concurrent.ConcurrentHashMap;

public class ServiceMapperConfigFactory implements VelocityTemplateConfigFactory<ServiceMapperConfig> {


    ConcurrentHashMap<String, ServiceMapperConfig> configHashMap = new ConcurrentHashMap<>();

    // **单例定义
    // 私有静态变量保存唯一实例
    private static volatile ServiceMapperConfigFactory instance = null;

    // **单例定义
    // 公共静态方法提供全局访问点
    public static ServiceMapperConfigFactory getInstance() {
        if (instance == null) {
            synchronized (XmlSubmitConfigFactory.class) {
                if (instance == null) {
                    instance = new ServiceMapperConfigFactory();
                    init();
                }
            }
        }
        return instance;
    }

    private static void init() {
        ServiceMapperConfig config = buildBasicConfig("contribute",
                "com.elecbook.content.service",
                Contribute.class);
        getInstance().addConfig("contribute", config);
    }

    private static ServiceMapperConfig buildBasicConfig(String tableName,
                                                        String servicePackageName, Class<?> clazzPo) {
        ServiceMapperConfig config = new ServiceMapperConfig();
        config.setTableName(tableName);
        config.setPackageName(servicePackageName);
        // domain的包名获取和设置
        String poPackage = clazzPo.getPackage().getName();
        String entityName = Tool.subStringBeforePunctuationCount(poPackage, ".", 1);
        config.setDomainName(entityName);
        config.innerGenerate();
        return config;
    }

    @Override
    public ServiceMapperConfig getConfig(String key) {
        return configHashMap.get(key);
    }

    @Override
    public void addConfig(String key, ServiceMapperConfig config) {
        configHashMap.put(key, config);
    }

    @Override
    public void addConfig(VelocityConfigManager manager) {
        ServiceMapperConfig config = buildBasicConfig(manager.getTableName(), manager.getServicePackageName(), manager.getClazzPo());
        getInstance().addConfig(manager.getTableName(), config);
    }

}
